package com.algo.count;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
 * get title from url list
 *
 */
public class GetTitleRunnable implements Runnable
{
	private static BufferedReader br;
	//private static boolean hasNext = true;
	
	private static BufferedWriter bw;
	
	
	public void run(){
		while(true){
			try{
				String url = null;
				synchronized(br){
					//if(hasNext)
						url = br.readLine();
					//else break;
					if(url == null){
						//hasNext = false;
						break;
					}
				}
		    	String title = getTitle(url);
	    		if(title != null)
	    			synchronized(bw){
	    				bw.write(url + "\t" + title + "\n");
	    			}
			}catch(IOException e){
				throw new RuntimeException(e);
			}
		}
	}
	public static void close(){
		try{
			if(br != null) br.close();
			if(bw != null)	bw.close();
		}catch(IOException e){
			throw new RuntimeException(e);
		}
	}
	public GetTitleRunnable(String in, String out){
		if(br == null || bw == null){
			try {
				br = new BufferedReader(new FileReader(in));
				bw = new BufferedWriter(new FileWriter(out));
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
	}
	public static String getTitle(String url){
		Document doc;
		
		try {
			new URL(url);
			Connection con = Jsoup.connect(url);
			con.header("user", "jhone");
			con.userAgent("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
			doc = con.get();
		} catch (IOException e) {
			System.out.println(url);
			return null;
		}
		
    	Elements heads = doc.getElementsByTag("head");
    	
    	StringBuilder sb = new StringBuilder();
    	
    	for(Element head : heads){
    		Elements titles = head.getElementsByTag("title");
    		for(Element title : titles){
    			sb.append(title.text());
    		}
    		
    		sb.append("\t");
    		Elements keys = head.getElementsByAttributeValue("name", "keywords");
    		for(Element key : keys){
    			sb.append(key.attr("content"));
    		}
    		
    		sb.append("\n");
    		Elements descs = head.getElementsByAttributeValue("name", "description");
    		for(Element desc : descs){
    			sb.append(desc.attr("content"));
    		}
    	}
    	
        return sb.length() > 3 ?sb.toString().replace("\n", " "):null;//
	}
	
    public static void main( String[] args ) throws InterruptedException
    {
    	//System.out.print(getTitle("http://10kn.com/visio-lixue/"));
    	//getTitles(args[0], args[1]);
    	
    	int threadsNum = Integer.parseInt(args[2]);
    	ExecutorService executor = Executors.newCachedThreadPool();
    	for(int i = 0; i < threadsNum; i++){
    		executor.execute(new GetTitleRunnable(args[0], args[1]));
    		System.out.println("thread " + i + " started");
    	}
    	System.out.println(executor.isTerminated());
	executor.shutdown();
	executor.awaitTermination(30, TimeUnit.DAYS);
	GetTitleRunnable.close();
    }
	
}

